package com.budwk.app.sqfwV2.generator.childrenVisitation.excel;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.LocalDateTimeUtil;
import com.alibaba.fastjson.JSONObject;
import com.budwk.app.sqfw.constants.CommonConstants;
import com.budwk.app.sqfw.models.Sqfw_formSummary_new;
import com.budwk.app.sqfwDoc.bo.SqfwExcelForSummaryBO;
import com.budwk.app.sqfwDoc.bo.SqfwExcelJudgeRes;
import com.budwk.app.sqfwDoc.codeGenerator.excelGenerate.BaseExcelParamDTO;
import com.budwk.app.sqfwDoc.codeGenerator.excelGenerate.BaseTemplateExcelGenerator;
import com.budwk.app.sqfwDoc.codeGenerator.excelGenerate.SqfwExcelCreateCommonBO;
import com.budwk.app.sqfwV2.component.DayingRoadPdfComponent;
import com.budwk.app.sqfwV2.dto.dto.childrenVisits.ChildrenVisitationPdfDTO;
import com.budwk.app.sqfwV2.dto.dto.childrenVisits.GuardianInfo;
import com.budwk.app.sqfwV2.dto.image.ExcelProblemImageConfigBO;
import com.budwk.app.sqfwV2.enums.FileServiceConfigEnum;
import com.budwk.app.util.StrUtil;
import com.budwk.app.util.poiUtils.excelPictureConfigBO.ExcelFileCreateParamBO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.time.LocalDate;
import java.time.temporal.ChronoUnit;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 儿童探访记录台账
 * @since 2024-04-23
 *
 * 白云区儿童探访记录 ChildrenVisitation
 * 儿童探访记录台账.xlsx
 */

@Slf4j
public class ChildrenVisitsLedger extends BaseTemplateExcelGenerator {


    @Override
    protected void customerPopularSheetBaseParam(Map<String, Object> excelParam, SqfwExcelCreateCommonBO excelCreateParam) {
        BaseExcelParamDTO baseParam = excelCreateParam.getBaseExcelParamDTO();
        if (StringUtils.isNotEmpty(baseParam.getCommunity()) && !CommonConstants.DIRECTLY_UNDER.equals(baseParam.getCommunity())) {
            excelParam.put("community", StrUtil.formatStr(baseParam.getCommunity()));
            return;
        }
        if (StringUtils.isNotEmpty(baseParam.getCounty()) && !CommonConstants.DIRECTLY_UNDER.equals(baseParam.getCounty())) {
            excelParam.put("community", StrUtil.formatStr(baseParam.getCounty()));
            return;
        }
        if (StringUtils.isNotEmpty(baseParam.getDistrict()) && !CommonConstants.DIRECTLY_UNDER.equals(baseParam.getDistrict())) {
            excelParam.put("community", StrUtil.formatStr(baseParam.getDistrict()));
            return;
        }
        excelParam.put("community", CommonConstants.HORIAONTAL_LINE);
    }

    @Override
    protected void customerPopularSheetDataParam(List<Map<String, Object>> listMap, SqfwExcelCreateCommonBO excelCreateParam) {
        List<Sqfw_formSummary_new> summaryList = DayingRoadPdfComponent.getSummaryList(listMap, excelCreateParam);
        log.info("ChildrenVisitsLedger customerDataParam summaryList:  {}", JSONObject.toJSONString(summaryList));
        int num = 1;
        for (Sqfw_formSummary_new summary : summaryList) {
            ChildrenVisitationPdfDTO roadPdfDTO = JSONObject.parseObject(summary.getParamData(), ChildrenVisitationPdfDTO.class);
            // 构建数据
            Map<String, Object> map = buildExcelMap(roadPdfDTO, summary);
            map.put("num", num ++);
            listMap.add(map);
        }
        log.info("ChildrenVisitsLedger customerDataParam end: {}", JSONObject.toJSONString(listMap));
    }

    private Map<String, Object> buildExcelMap(ChildrenVisitationPdfDTO roadPdfDTO, Sqfw_formSummary_new summary) {
        Map<String, Object> map = new HashMap<>();
        map.put("visitedName", StrUtil.formatStr(roadPdfDTO.getVisitedName()));
        map.put("citizenSex", StrUtil.formatStr(roadPdfDTO.getCitizenSex()));
        map.put("age", "");
        if (roadPdfDTO.getBirthday() != null) {
            map.put("age", ChronoUnit.YEARS.between(roadPdfDTO.getBirthday(), LocalDate.now()) + "岁");
        }
        map.put("promotionSummary", StrUtil.formatStr(roadPdfDTO.getPromotionSummary()));
        map.put("createName", StrUtil.formatStr(summary.getCreateUserName()));

        map.put("guardianInfoName", "");
        List<GuardianInfo> guardianInfos = roadPdfDTO.getGuardianInfos();
        if (guardianInfos != null && guardianInfos.size() > 0) {
            GuardianInfo guardianInfo = guardianInfos.get(0);
            map.put("guardianInfoName", StrUtil.formatStr(guardianInfo.getName()));
        }
        map.put("fillDate", LocalDateTimeUtil.format(roadPdfDTO.getFillDate(), DatePattern.CHINESE_DATE_PATTERN));
        return map;
    }

    public boolean isGenerate() {
        return true;
    }

    @Override
    protected String getTemplateName() {
        return FileServiceConfigEnum.CB.getFilePath();
    }

    @Override
    public String getDownloadFileName() {
        return FileServiceConfigEnum.CB.getFileName();
    }

    @Override
    protected void completeExcelFileCreateParam(ExcelFileCreateParamBO excelFileCreateParam) {

    }

    @Override
    protected void customerPopularRowDataMap(Map<String, Object> rowDataMap, SqfwExcelForSummaryBO forSummaryBO) {

    }

    @Override
    protected SqfwExcelJudgeRes judgeDataIntoExcelBeforeErgodic(SqfwExcelCreateCommonBO excelCreateParam, Map<String, Object> excelParam) {
        return null;
    }

    @Override
    protected SqfwExcelJudgeRes judgeDataIntoExcelBeforeCreate(Map<String, Object> excelParam) {
        return null;
    }

    @Override
    protected Class<?> getImageConfigBO() {
        return ExcelProblemImageConfigBO.class;
    }
}
